package org.xx.armory.spring5.mvc;

import java.io.Serializable;
import java.math.BigDecimal;

/**
 * 用于在页面上显示的金额。
 */
public final class Money
        implements Serializable {
    private static final long serialVersionUID = 1L;

    private final BigDecimal value;

    public Money() {
        this(BigDecimal.ZERO);
    }

    public Money(
            BigDecimal value
    ) {
        this.value = value != null ? value : BigDecimal.ZERO;
    }

    public static Money valueOf(
            int v
    ) {
        return new Money(BigDecimal.valueOf(v));
    }

    public static Money valueOf(
            long v
    ) {
        return new Money(BigDecimal.valueOf(v));
    }

    public static Money valueOf(
            float v
    ) {
        return new Money(BigDecimal.valueOf(v));
    }

    public static Money valueOf(
            double v
    ) {
        return new Money(BigDecimal.valueOf(v));
    }

    public static Money valueOf(
            BigDecimal v
    ) {
        return new Money(v);
    }

    public static Money valueOf(
            String v
    ) {
        return new Money(new BigDecimal(v));
    }

    public final BigDecimal value() {
        return this.value;
    }

    public final boolean isPositive() {
        return this.value.signum() > 0;
    }

    public final boolean isNegative() {
        return this.value.signum() < 0;
    }

    public final boolean isZero() {
        return this.value.signum() == 0;
    }

    @Override
    public final int hashCode() {
        return this.value.hashCode();
    }

    @Override
    public final boolean equals(
            Object other
    ) {
        return other instanceof Money && this.value.equals(((Money) other).value);
    }

    @Override
    public final String toString() {
        return this.value.toString();
    }
}
